package com.light.gamecomponents.physics;

import static org.anddev.andengine.extension.physics.box2d.util.constants.PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;

import org.anddev.andengine.audio.sound.Sound;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.light.gamecomponents.CullingSprite;
import com.light.roller.Direction;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;

public class DeathNeedle extends PhysicsComponents implements ContactListener{
	private Sound m_collideSound;
	private Sprite temp;
	Direction m_direction;

	public DeathNeedle(Roller game, String resourceName,Vector2 initialPosition, Vector2 size,Direction direction) {
		// TODO Auto-generated constructor stub
		super(game, resourceName, initialPosition, size);
		m_direction = direction;
		
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {

	}

	@Override
	public void reset() {
		
	}

	@Override
	public void onLoadResources() {
		ResourceManager.onLoadResources(m_resourceName, m_game);
		m_textureRegion = ResourceManager.getTextureRegion(m_resourceName);
	}
	
	@Override
	public void unLoadResources() {
		m_textureRegion = null;
		ResourceManager.unLoadResources(m_resourceName, m_game);
	}

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWorld) {
		FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(30, 0.0f, 1.0f);
		
		objectFixtureDef.filter.categoryBits = Roller.CATEGORYBIT_NORMAL;
		objectFixtureDef.filter.maskBits = Roller.MASKBIT_NORMAL;
		
		m_sprite = new CullingSprite(m_initialPosition.x, 
				  		      m_initialPosition.y,
                              m_textureRegion);
		temp=new Sprite(m_initialPosition.x, 
	  		      			   m_initialPosition.y,
	  		      			   m_textureRegion);
		temp.setScale(0.7f);
		setSpriteDirection();
		
		Vector2[] vertices = getPointsPosition();
		
		m_body = PhysicsFactory.createPolygonBody(physicsWorld,
												  temp, 
												  vertices, 
												  BodyType.StaticBody, 
												  objectFixtureDef);
		
		
		
		scene.attachChild(m_sprite);
		m_physicsConnector = new PhysicsConnector(m_sprite, m_body, true, true);
		
		if(m_direction == Direction.LEFT)
		{
			m_body.setTransform(m_body.getPosition(), 3.14f/2);
		}else if(m_direction == Direction.RIGHT)
		{
			m_body.setTransform(m_body.getPosition(), 3.14f*3/2);
		}
		
		physicsWorld.registerPhysicsConnector(m_physicsConnector);
		((RollerPhysicsWorld)physicsWorld).registerContactListener(this);
		
		
	}

	private void setSpriteDirection() {
		// TODO Auto-generated method stub
		switch(m_direction)
		{
		case RIGHT:
			m_sprite.setRotation(90f);
			temp.setRotation(90f);
			break;
		case UP:
			
			break;
		case LEFT:
			m_sprite.setRotation(270f);
			temp.setRotation(270f);
			break;
		case DOWN:
			m_sprite.getTextureRegion().setFlippedVertical(true);

			break;
		}
		
		
	}

	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) 
	{
		physicsWorld.unregisterPhysicsConnector(m_physicsConnector);
		scene.detachChild(m_sprite);
		physicsWorld.destroyBody(m_body);
	
	}

	@Override
	public void onLoadEngine() {
		
	}

	@Override
	public void setPosition(int x, int y) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beginContact(Contact contact) {
		if ((contact.getFixtureA().getBody() == this.m_body || contact.getFixtureB().getBody() == this.m_body)
			&&
			(contact.getFixtureA().getBody() == Roller.player.getBody() || contact.getFixtureB().getBody() == Roller.player.getBody()))
			{
				Roller.player.kill();
			}
	}

	@Override
	public void endContact(Contact contact) {
		// TODO Auto-generated method stub
	}

	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}	
	
	private Vector2[] getPointsPosition()
	{
		final float halfWidth = temp.getWidthScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
		final float halfHeight = temp.getHeightScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
		
		Vector2[] vertices = null;
		
		switch(m_direction)
		{
		case RIGHT:
			Vector2[] verticesRight = {
					new Vector2(0, -halfHeight),
					new Vector2(halfWidth, halfHeight),
					new Vector2(-halfWidth, halfHeight)
			};
			vertices = verticesRight;
			break;
		case UP:
			Vector2[] verticesUp = {
					new Vector2(0, -halfHeight),
					new Vector2(halfWidth, halfHeight),
					new Vector2(-halfWidth, halfHeight)
			};
			vertices = verticesUp;
			break;
		case LEFT:
			Vector2[] verticesLeft = {
					new Vector2(0, -halfHeight),
					new Vector2(halfWidth, halfHeight),
					new Vector2(-halfWidth, halfHeight)
			};
			vertices = verticesLeft;
			break;
			
		case DOWN:
			Vector2[] verticesDown = {
					new Vector2(-halfWidth,-halfHeight),
					new Vector2(halfWidth,-halfHeight),
					new Vector2(0,halfHeight)
			};
			vertices = verticesDown;
			break;
		}
		
		
		return vertices;
		
	}
	@Override
	public void stopBody()
	{

	}
	@Override
	public void startBody()
	{

	}
}

